package noobteam.deartrainer.controller;

import noobteam.deartrainer.service.BudgetInfoService;
import noobteam.deartrainer.service.UserInfoService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 徐紫印
 */
@RestController
public class BudgetInfoController {

    @Resource
    private BudgetInfoService budgetInfoService;
    @Resource
    private UserInfoService userInfoService;

    /**
     * 后端内部函数
     * @param budget 交易信息
     * @return 成功或失败信息
     */
    @RequestMapping("/createBudget")
    public String createBudget(@RequestParam Map<String, Object> budget){
        int id = (int)budget.get("userId");
        double currentUseBalance = userInfoService.getUserAccount(id);
        budget.put("currentUseBalance", currentUseBalance);
        boolean isSucceed = budgetInfoService.createBudget(budget);
        if (isSucceed) {
            return budget.get("itemType") + "成功";
        }
        return budget.get("itemType") + "失败";
    }

    @RequestMapping("/getUserBudget")
    public List<Map<String, Object>> getUserBudget(@RequestParam Integer userId) {
        return budgetInfoService.getBudgetInfo(userId);
    }
}
